//
//  SelectModel.m
//  LiWuShuo
//
//  Created by 袁小航 on 14/11/13.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SelectModel.h"
#import "AFAppDotNetAPIClient.h"

@implementation SelectModel

-(instancetype)initWithAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    self.selImageUrl = [attributes valueForKey:@"cover_image_url"];
    self.selInfo = [attributes valueForKey:@"title"];
    self.selCollectNum = [attributes valueForKey:@"likes_count"];
    self.subUrl = [attributes valueForKey:@"content_url"];
    
    return self;
}
+ (NSURLSessionDataTask *)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block
                                                  page:(NSInteger)page
{
    return [[AFAppDotNetAPIClient sharedClient] GET:[NSString stringWithFormat:@"http://api.liwushuo.com/v1/channels/1/items?limit=12&offset=%d", page] parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSArray *postsFromResponse = [[JSON valueForKey:@"data"] valueForKey:@"items"];
        NSMutableArray * mutablePosts = [NSMutableArray array];
        for (NSDictionary *attributes in postsFromResponse) {
            SelectModel *model = [[SelectModel alloc]initWithAttributes:attributes];
            [mutablePosts addObject:model];
        }
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}


- (instancetype)initWithTopAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    self.selTopImageUrl = [attributes valueForKey:@"image_url"];
    self.selTopID = [attributes valueForKey:@"target_id"];
    
    return self;
}
- (instancetype)initWithTopicAttributes:(NSDictionary *)attributes
{
    self = [self init];
    if (!self) {
        return nil;
    }
    self.selTopImageUrl = [attributes valueForKey:@"image_url"];
    self.selTopicId = [[attributes valueForKey:@"target"] objectForKey:@"id"];
    self.selTopicTitle = [[attributes valueForKey:@"target"] objectForKey:@"title"];
    
    return self;
}
+ (NSURLSessionDataTask *)selectionTopImageWithBlock:(void (^)(NSArray *posts, NSError *errot))block
{
    return [[AFAppDotNetAPIClient sharedClient] GET:@"http://api.liwushuo.com/v1/banners" parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSArray *postsFromResponse = [[JSON valueForKey:@"data"] valueForKey:@"banners"];
        NSMutableArray * mutablePosts = [NSMutableArray array];
        for (NSDictionary *attributes in postsFromResponse) {
            if ([attributes objectForKey:@"target"] == nil) {
                SelectModel *model1 = [[SelectModel alloc]initWithTopAttributes:attributes];
                [mutablePosts addObject:model1];
            } else {
                SelectModel *model2 = [[SelectModel alloc]initWithTopicAttributes:attributes];
                [mutablePosts addObject:model2];
            }
        }
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}


@end
